%% set parameters
maxMinLS = 20;
minLS = optimizableVariable('minLS',[1,maxMinLS],'Type','integer');
numPTS = optimizableVariable('numPTS',[1,size(X,2)-1],'Type','integer');
hyperparametersRF = [minLS; numPTS];

str_method = 'regression'; % or 'classification'
NumTrees = 200;

%% bayesian optimization
results = bayesopt(@(params)oobErrRF(params,X,y),hyperparametersRF,...
    'AcquisitionFunctionName','expected-improvement-plus','Verbose',0);

%% display results
bestOOBErr = results.MinObjective
bestHyperparameters = results.XAtMinObjective

%% re-train with optimized parameters
Mdl = TreeBagger(NumTrees,X,y,'Method',str_method,...
    'MinLeafSize',bestHyperparameters.minLS,...
    'NumPredictorstoSample',bestHyperparameters.numPTS);
